package com.example.springboot.controller;

import com.example.springboot.annotation.MethodPermission;
import com.example.springboot.common.Result.Result;
import com.example.springboot.entity.Permission;
import com.example.springboot.entity.dto.AssginMenuDto;
import com.example.springboot.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/acl/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 获取菜单列表
     */
    @MethodPermission("menu_query")
    @GetMapping()
    public Result<?> list() {
        List<Permission> menus = permissionService.findNodes();
        return Result.success(menus);
    }

    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId) {
        List<Permission> list = permissionService.findMenuByRoleId(roleId);
        return Result.success(list);
    }

    @MethodPermission("role_update")
    @PostMapping("/doAssign")
    public Result doAssign(AssginMenuDto assginMenuDto) {
        permissionService.doAssign(assginMenuDto);
        return Result.success();
    }

    @MethodPermission("menu_add")
    @PostMapping("save")
    public Result save(@RequestBody Permission permission) {
        permissionService.save(permission);
        return Result.success();
    }


    @MethodPermission("menu_update")
    @PutMapping("update")
    public Result updateById(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return Result.success();
    }

    @MethodPermission("menu_delete")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        permissionService.removeMenuById(id);
        return Result.success();
    }
}
